﻿using BadOCR.Runtime;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace BadOCR.UserControls
{
    /// <summary>
    /// TranslateUserControl.xaml 的交互逻辑
    /// </summary>
    public partial class TranslateUserControl : UserControl
    {
        public bool MonitorClipboard => (bool)checkbox_monitor_clipboard.IsChecked;

        public TranslateUserControl()
        {
            InitializeComponent();
        }

        private void textbox_searchContent_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.Key == Key.Enter)
            {
                InternalTranslate();
            }
        }

        public void Translate(string originText)
        {
            //originText = originText.Replace("\r", "").Replace("\n", "");    // 搜索之前去掉所有的换行符
            if (string.IsNullOrEmpty(originText))
                return;

            if (textbox_searchContent.Text == originText)
                return;

            textbox_searchContent.Text = originText;

            InternalTranslate();
        }

        private void InternalTranslate()
        {
            var originText = textbox_searchContent.Text?.Trim();
            if (string.IsNullOrEmpty(originText))
                return;

            // 检测源语言
            var srcLanguage = LanguageHelper.LanguageDetect(originText);

            // 计算目标语言
            var targetLanguage = LanguageHelper.GetTargetLanguage(srcLanguage);     // 暂时写死, 以后做成配置

            // 开始翻译
            if (targetLanguage != srcLanguage)
            {
                google_translator_control.Translate(originText, targetLanguage);
                youdao_translator_control.Translate(originText, targetLanguage);
                bing_translator_control.Translate(originText, targetLanguage);
            }
        }
    }
}
